//
//  MyClassRoomViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//  收藏课程

#import "MyClassRoomViewController.h"
#import "ClassRoomTableViewCell.h"
#import "LiveDetailViewController.h"
#import "ClassRoomDetailViewController.h"

@interface MyClassRoomViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation MyClassRoomViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleStr;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ClassRoomTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ClassRoomCell"];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:self.classRoomType == KHistoryClassRoom ?@"清空历史":@"清空收藏" target:self selector:@selector(deleteAll)];
    [self setupRefresh];
}
#pragma mark - 清空数据
- (void)deleteAll{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    NSString *url = self.classRoomType == KHistoryClassRoom ? [JSPathUtil urlAppCourseDeleteHistoryList]:[JSPathUtil urlAppCourseDeleteAllFavourite];
   
    [yjNetWorkManger requestType:POST andURL:url andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"清空成功"];
            [self.dataArray removeAllObjects];
            [self.tableView reloadData];
        }
    }];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo] forKey:@"page"];   //页数 默认1
    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    NSString *url = [JSPathUtil urlAppCourseGetFavouriteList];
    if (self.classRoomType == KHistoryClassRoom) {
        url = [JSPathUtil urlAppCourseGetCourseHistoryList];
    }
    [yjNetWorkManger requestType:POST andURL:url andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray *array = [NSArray arrayWithArray:response[@"list"]];
            for (int i = 0; i < array.count; i++) {
                [self.dataArray addObject:[ClassRoomInfo mj_objectWithKeyValues:array[i]]];
            }
            [self refreshCount:self.dataArray.count noDataText:self.classRoomType == KHistoryClassRoom ?@"还没有历史课程":@"还没有收藏的课程" totalRow:response[@"totalRow"]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassRoomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ClassRoomCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.classRoomInfo = self.dataArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassRoomInfo *classRoomInfo = self.dataArray[indexPath.section];
    if ([classRoomInfo.videoType isEqualToString:@"1"]) {
        LiveDetailViewController *liveDetailVC = [[LiveDetailViewController alloc]init];
        liveDetailVC.liveDetailId = classRoomInfo.course_id;
        [self.navigationController pushViewController:liveDetailVC animated:YES];
    }else{
        ClassRoomDetailViewController *classRoomDetailVC = [[ClassRoomDetailViewController alloc]init];
        classRoomDetailVC.courseId = classRoomInfo.course_id;
        [self.navigationController pushViewController:classRoomDetailVC animated:YES];
    }
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 0.01);
    //    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
